package com.zhongwang.mms.module.overview.service;

import com.zhongwang.mms.gen.entity.TProStatusChangeRecord;
import com.zhongwang.mms.module.basic.dao.TProjectDao;
import com.zhongwang.mms.module.overview.dao.OveMatStatusQueryDao;
import com.zhongwang.mms.module.overview.dao.ProjectStateDao;
import com.zhongwang.mms.module.overview.model.ProjectStatusModel;
import com.zhongwang.mms.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.zhongwang.mms.config.timingtask.StatisticalTimingTask.getVWProjectByCode;

/**
 * @Description: 生产状态编辑 Service
 * @Author: China.sgl
 * @Date: 2020/4/7 16:25
 */
@SuppressWarnings("ALL")
@Service
public class ProjectStateService {

    @Autowired
    private ProjectStateDao projectStateDao;
    @Autowired
    private TProjectDao tProjectDao;
    @Autowired
    private OveMatStatusQueryDao oveMatStatusQueryDao;

    /**
     * 查询项目
     *
     * @param projectStatusModel 条件
     * @return list
     */
    public List<ProjectStatusModel> findProject(ProjectStatusModel projectStatusModel) {
        return projectStateDao.findProject(projectStatusModel);
    }

    /**
     * 查询项目状态
     *
     * @param id 主键
     * @return ReportPlanModel
     */
    public ProjectStatusModel findProjectStateById(Integer id) {
        return projectStateDao.findProjectStateById(id);
    }

    /**
     * 修改项目状态
     *
     * @param projectStatusModel 项目状态
     * @return 行数
     */
    public int updateProjectStatus(ProjectStatusModel projectStatusModel) {
        if (projectStatusModel.getProId() == null) {
            projectStatusModel.setProProjectId(projectStatusModel.getId());
            projectStatusModel.setProCode(projectStatusModel.getCode());
            projectStatusModel.setProStatus(1);
            return projectStateDao.insertProjectState(projectStatusModel);
        }
        return projectStateDao.updateProjectState(projectStatusModel);
    }

    /**
     * 修改项目物控科状态
     *
     * @param projectStatusModel 项目状态
     * @return 行数
     */
    //@Transactional(rollbackFor = Exception.class)
    public int saveMcData(ProjectStatusModel projectStatusModel) {
        if (projectStatusModel.getProId() == null) {
            int result = 0;
            projectStatusModel.setProProjectId(projectStatusModel.getId());
            projectStatusModel.setProCode(projectStatusModel.getCode());
            projectStatusModel.setProStatus(1);
            result += projectStateDao.insertProjectMcState(projectStatusModel);
            Date insertDate = new Date();
            TProStatusChangeRecord rawMat = new TProStatusChangeRecord();
            rawMat.setPscProCode(projectStatusModel.getCode());
            rawMat.setPscProName(getVWProjectByCode(projectStatusModel.getCode()).getName());
            rawMat.setPscStatusType("原材料");
            rawMat.setPscBeforeStatus(0);
            rawMat.setPscAfterStatus(projectStatusModel.getProMatStatus());
            rawMat.setPscChangeDate(insertDate);
            rawMat.setPscChangeOperatorId(ShiroUtils.getUserId());
            rawMat.setPscChangeOperator(ShiroUtils.getUser().getName());
            result += oveMatStatusQueryDao.insert(rawMat);
            TProStatusChangeRecord pur = new TProStatusChangeRecord();
            pur.setPscProCode(projectStatusModel.getCode());
            pur.setPscProName(getVWProjectByCode(projectStatusModel.getCode()).getName());
            pur.setPscStatusType("外购件");
            pur.setPscBeforeStatus(0);
            pur.setPscAfterStatus(projectStatusModel.getProPurStatus());
            pur.setPscChangeDate(insertDate);
            pur.setPscChangeOperatorId(ShiroUtils.getUserId());
            pur.setPscChangeOperator(ShiroUtils.getUser().getName());
            result += oveMatStatusQueryDao.insert(pur);
            TProStatusChangeRecord subcontract = new TProStatusChangeRecord();
            subcontract.setPscProCode(projectStatusModel.getCode());
            subcontract.setPscProName(getVWProjectByCode(projectStatusModel.getCode()).getName());
            subcontract.setPscStatusType("外协件");
            subcontract.setPscBeforeStatus(0);
            subcontract.setPscAfterStatus(projectStatusModel.getProSubcontractStatus());
            subcontract.setPscChangeDate(insertDate);
            subcontract.setPscChangeOperatorId(ShiroUtils.getUserId());
            subcontract.setPscChangeOperator(ShiroUtils.getUser().getName());
            result += oveMatStatusQueryDao.insert(subcontract);
            return result;
        } else {
            int result = 0;
            ProjectStatusModel before = projectStateDao.getProjectModel(projectStatusModel.getProId());
            result += projectStateDao.updateProjectMcState(projectStatusModel);
            ProjectStatusModel after = projectStateDao.getProjectModel(projectStatusModel.getProId());
            Date changeDate = new Date();
            if(!before.getProMatStatus().equals(after.getProMatStatus())){
                TProStatusChangeRecord rawMat = new TProStatusChangeRecord();
                rawMat.setPscProCode(projectStatusModel.getCode());
                rawMat.setPscProName(getVWProjectByCode(projectStatusModel.getCode()).getName());
                rawMat.setPscStatusType("原材料");
                rawMat.setPscBeforeStatus(before.getProMatStatus());
                rawMat.setPscAfterStatus(after.getProMatStatus());
                rawMat.setPscChangeDate(changeDate);
                rawMat.setPscChangeOperatorId(ShiroUtils.getUserId());
                rawMat.setPscChangeOperator(ShiroUtils.getUser().getName());
                result += oveMatStatusQueryDao.insert(rawMat);
            }
            if(!before.getProPurStatus().equals(after.getProPurStatus())){
                TProStatusChangeRecord pur = new TProStatusChangeRecord();
                pur.setPscProCode(projectStatusModel.getCode());
                pur.setPscProName(getVWProjectByCode(projectStatusModel.getCode()).getName());
                pur.setPscStatusType("外购件");
                pur.setPscBeforeStatus(before.getProPurStatus());
                pur.setPscAfterStatus(after.getProPurStatus());
                pur.setPscChangeDate(changeDate);
                pur.setPscChangeOperatorId(ShiroUtils.getUserId());
                pur.setPscChangeOperator(ShiroUtils.getUser().getName());
                result += oveMatStatusQueryDao.insert(pur);
            }
            if(!before.getProSubcontractStatus().equals(after.getProSubcontractStatus())){
                TProStatusChangeRecord subcontract = new TProStatusChangeRecord();
                subcontract.setPscProCode(projectStatusModel.getCode());
                subcontract.setPscProName(getVWProjectByCode(projectStatusModel.getCode()).getName());
                subcontract.setPscStatusType("外协件");
                subcontract.setPscBeforeStatus(before.getProSubcontractStatus());
                subcontract.setPscAfterStatus(after.getProSubcontractStatus());
                subcontract.setPscChangeDate(changeDate);
                subcontract.setPscChangeOperatorId(ShiroUtils.getUserId());
                subcontract.setPscChangeOperator(ShiroUtils.getUser().getName());
                result += oveMatStatusQueryDao.insert(subcontract);
            }
            return result;
        }
    }
}
